﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using DTcms.BLL;
using DTcms.Common;
using DTcms.Web.interfaceDOC;
using Newtonsoft.Json;

namespace DTcms.Web.WxSdjs
{
    /// <summary>
    /// UserHandler 的摘要说明
    /// </summary>
    public class UserHandler : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            BizUserRegister bizUserRegister = new BizUserRegister();
            Result result = new Result();
            string type = context.Request["type"];//1:跑男, 2:用户             
            string phone = context.Request.Params["phone"]; //手机号
            string voidCode = context.Request.Params["voidCode"]; //验证码
            string pwd = context.Request.Params["Pwd"]; //密码


            object newVilidCode = context.Session["newVilidCode"];

#if DEBUG
            newVilidCode = "759069";
            context.Session["enDescPeiSongId"] = context.Request["recommenderId"];
            context.Session["pao_type"] = context.Request["recommendType"];
#endif


            if (newVilidCode == null || voidCode != newVilidCode.ToString())
            {
                result.Code = 2;
                result.Msg = "验证码不正确";
            }
            else
            {
                //  推荐人Id
                long recommenderId = -1;
                if (context.Session["enDescPeiSongId"] != null)
                    long.TryParse(context.Session["enDescPeiSongId"].ToString(), out recommenderId);
                //  推荐类型
                int recommendType = 2;
                if (context.Session["pao_type"] != null)
                    int.TryParse(context.Session["pao_type"].ToString(), out recommendType);

                if (type == "1")
                {
                    result = bizUserRegister.Running(recommenderId, recommendType, phone, pwd);
                }
                else if (type == "2")
                {
                    result = bizUserRegister.User(recommenderId, recommendType, phone, pwd);
                }
            }


            context.Response.Write(JsonConvert.SerializeObject(result));
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }



    }
}